package com.example.baserecyclerviewadapterdemo.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.adapter.AnimationAdapter;
import com.example.baserecyclerviewadapterdemo.anim.CustomAnimation1;
import com.example.baserecyclerviewadapterdemo.anim.CustomAnimation2;
import com.example.baserecyclerviewadapterdemo.data.DataServer;
import com.example.baserecyclerviewadapterdemo.entity.Status;
import com.jaredrummler.materialspinner.MaterialSpinner;
import com.kyleduo.switchbutton.SwitchButton;

public class AnimationUseActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private AnimationAdapter mAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adapter_use);

        final Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initMenus();

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new AnimationAdapter(R.layout.layout_animation, DataServer.getSampleData(100));
        mAdapter.setAnimationEnable(true);
        recyclerView.setAdapter(mAdapter);

        int mFirstPageItemCount = 3;
        mAdapter.addChildClickViewIds(R.id.img,R.id.tweetName,R.id.tweetText);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                String content = null;
                Status status = mAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.img:
                        content = "img: " + status.getUserAvatar();
                        break;
                    case R.id.tweetName:
                        content = "tweetName: " + status.getUserName();
                        break;
                    case R.id.tweetText:
                        content = "tweetText: " + status.getText();
                        break;
                }
                Toast toast = Toast.makeText(AnimationUseActivity.this, content, Toast.LENGTH_SHORT);
                toast.setText(content);
                toast.show();
            }
        });

        SwitchButton switchButton = findViewById(R.id.switch_button);
        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mAdapter.setAnimationFirstOnly(true);
                }else{
                    mAdapter.setAnimationFirstOnly(false);
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initMenus(){
        MaterialSpinner spinner = findViewById(R.id.spinner);
        spinner.setItems("AlphaIn", "ScaleIn", "SlideInBottom", "SlideInLeft", "SlideInRight", "Custom1", "Custom2");
        spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                switch (position) {
                    case 0:
                        mAdapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.AlphaIn);
                        break;
                    case 1:
                        mAdapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.ScaleIn);
                        break;
                    case 2:
                        mAdapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInBottom);
                        break;
                    case 3:
                        mAdapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInLeft);
                        break;
                    case 4:
                        mAdapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInRight);
                        break;
                    case 5:
                        mAdapter.setAdapterAnimation(new CustomAnimation1());
                        break;
                    case 6:
                        mAdapter.setAdapterAnimation(new CustomAnimation2());
                        break;
                }
                recyclerView.setAdapter(mAdapter);
            }
        });
    }
}
